package com.shujia.zookeeper;

import org.apache.zookeeper.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.omg.CORBA.TRANSACTION_MODE;

import java.io.IOException;
import java.util.List;

public class Demo1Zk {
    ZooKeeper zk;

    @Before
    public void init() {
        //1、创建连接
        try {
            zk = new ZooKeeper("192.168.129.101:2181,192.168.129.102:2181,192.168.129.103:2181", 1000, null);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    // 永久性节点
    @Test
    public void createPers() {
        //2、创建节点
        try {
            String s = zk.create(
                    "/java2",// 节点路径
                    "shujia".getBytes(), // 节点数据
                    ZooDefs.Ids.OPEN_ACL_UNSAFE,  //权限
                    CreateMode.PERSISTENT_SEQUENTIAL // 节点类型
            );
            System.out.println(s);
        } catch (KeeperException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


    //临时节点   连接断开自动删除
    @Test
    public void createEphe() {
        try {
            zk.create("/hadoop/master",
                    "hdfs".getBytes(),
                    ZooDefs.Ids.OPEN_ACL_UNSAFE,
                    CreateMode.EPHEMERAL//临时节点
            );


            while (true) {

            }

        } catch (KeeperException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


    @Test
    public void get() {
        try {
            byte[] javas = zk.getData("/java", true, null);

            String string = new String(javas);

            System.out.println(string);

        } catch (KeeperException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


    @Test
    public void mdfily() {

        try {
            zk.setData("/java", "bigdata".getBytes(), -1);

        } catch (KeeperException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }


    @Test
    public void Watcher() {
        try {

            List<String> children = zk.getChildren("/hadoop", new Watcher() {

                /**
                 * 当java 子节点发生变化后会回掉process 方法，   这个监听是一次性的
                 *
                 * @param watchedEvent
                 */
                @Override
                public void process(WatchedEvent watchedEvent) {
                    System.out.println(watchedEvent);
                    System.out.println("节点被修改");

                    //在回调方法中实现namenode的主备切换
                }
            });

            System.out.println(children);

            /**
             * 绑定事件方法
             *
             */

//            zk.getChildren();
//            zk.getData();
//            zk.exists();


            while (true) {

            }


        } catch (KeeperException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


    @After
    public void close() {
        try {
            zk.close();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


}
